index.tsx 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507
  1. "use client";
  2. import { loginApi, registerApi } from "@/api/login";
  3. import ButtonOwn from "@/components/ButtonOwn";
  4. import MobileField from "@/components/Fields/MobileField";
  5. import { useEventPoint } from "@/hooks/useEventPoint";
  6. import { Link, useRouter } from "@/i18n/routing";
  7. import { useSystemStore } from "@/stores/useSystemStore";
  8. import { useUserInfoStore } from "@/stores/useUserInfoStore";
  9. import { emailReg, neReg } from "@/utils";
  10. import { setCookies } from "@/utils/Cookies";
  11. import { Local } from "@/utils/storage";
  12. import {
  13. Checkbox,
  14. DatePicker,
  15. DatePickerRef,
  16. Form,
  17. Input,
  18. Radio,
  19. Space,
  20. TextArea,
  21. Toast,
  22. } from "antd-mobile";
  23. import { PickerDate } from "antd-mobile/es/components/date-picker/util";
  24. import clsx from "clsx";
  25. import dayjs from "dayjs";
  26. import { useTranslations } from "next-intl";
  27. import { useSearchParams } from "next/navigation";
  28. import { FC, RefObject, useEffect, useRef, useState } from "react";
  29. import styles from "./style.module.scss";
  30. interface MobileFieldProps {
  31. value?: string;
  32. onChange?: (value: string) => void;
  33. }
  34. // const MobileField: FC<MobileFieldProps> = (props) => {
  35. // const t = useTranslations("form");
  36. // const { value, onChange } = props;
  37. //
  38. // const changeHandler = (value: string) => {
  39. // let newAmount = value.replace(/[^0-9]/g, "");
  40. // if (onChange) {
  41. // onChange(newAmount);
  42. // }
  43. // };
  44. // return (
  45. // <>
  46. // <Space align="center" className={"text-[#ccc]"}>
  47. // <Space align="center">+55</Space>
  48. // <Input
  49. // placeholder={t("phone")}
  50. // maxLength={11}
  51. // value={value}
  52. // onChange={(e) => changeHandler(e)}
  53. // />
  54. // </Space>
  55. // </>
  56. // );
  57. // };
  58. const SexMobile: FC<MobileFieldProps> = (props) => {
  59. const { value, onChange } = props;
  60. const handler = () => {
  61. console.log(`🚀🚀🚀🚀🚀-> in index.tsx on 58`, 123);
  62. };
  63. return (
  64. <div className={"w-[100%] border-4"} onClick={handler}>
  65. <Space align="center" className={"border-4 text-[#ccc]"}>
  66. <div className={"h-full w-[100%]"}>{value}</div>
  67. </Space>
  68. </div>
  69. );
  70. };
  71. interface FormProps {
  72. /**
  73. * 通讯地址
  74. */
  75. address?: string;
  76. /**
  77. * 用户头像
  78. */
  79. avatar_url?: string;
  80. /**
  81. * 生日:yyyy-MM-dd
  82. */
  83. birthday?: string;
  84. /**
  85. * 邮箱地址
  86. */
  87. email?: string;
  88. /**
  89. * 真实姓名
  90. */
  91. nick_name?: string;
  92. /**
  93. * 外部ID
  94. */
  95. open_id?: string;
  96. /**
  97. * 身份护照
  98. */
  99. passport?: string;
  100. /**
  101. * 用户密码
  102. */
  103. pwd: string;
  104. /**
  105. * 推荐码
  106. */
  107. referrer_code?: string;
  108. /**
  109. * 用户名
  110. */
  111. user_name?: string;
  112. /**
  113. * 用户电话号码
  114. */
  115. user_phone: string;
  116. /**
  117. * 用户类型
  118. */
  119. user_type?: string;
  120. sex?: number;
  121. /**
  122. * 渠道链接
  123. */
  124. channel_code?: string;
  125. code_phone?: string;
  126. first_name?: string;
  127. last_name?: string;
  128. }
  129. interface FormInitStateTypes extends FormProps {
  130. mobile?: {
  131. preValue: string;
  132. realValue: string;
  133. };
  134. }
  135. export type FormType = "login" | "register";
  136. interface Props {
  137. type?: FormType;
  138. }
  139. const FormComponent: FC<Props> = (props) => {
  140. const { type = "register" } = props;
  141. const isStrictMode = useSystemStore().identity_verify.register === 1;
  142. const { setUserInfo } = useUserInfoStore();
  143. const t = useTranslations();
  144. const searchParams = useSearchParams();
  145. const [statusText, setStatusText] = useState("");
  146. const { eventLogin, eventRegister } = useEventPoint();
  147. const formRef = useRef<any>(null);
  148. /// 密码可见
  149. const [visible, setVisible] = useState(false);
  150. const spanClassName = clsx("iconfont", {
  151. "icon-kejian": visible,
  152. "icon-bukejian": !visible,
  153. });
  154. // 是否同意协议
  155. const [checkBoxValue, setCheckBoxValue] = useState<boolean>(false);
  156. const router = useRouter();
  157. /// 初始值
  158. const params = useRef<FormInitStateTypes>({
  159. user_phone: "",
  160. mobile: { preValue: "55", realValue: "" },
  161. pwd: "",
  162. sex: 0,
  163. address: undefined,
  164. birthday: undefined,
  165. email: undefined,
  166. passport: undefined,
  167. referrer_code: undefined,
  168. });
  169. useEffect(() => {
  170. formRef.current?.resetFields();
  171. }, [type]);
  172. const onFinish = (values: FormInitStateTypes) => {
  173. const { mobile } = values;
  174. const newValue = {
  175. ...values,
  176. user_phone: `${mobile?.preValue}${mobile?.realValue}`,
  177. code_phone: mobile?.preValue,
  178. };
  179. delete newValue.mobile;
  180. if (isStrictMode) {
  181. strictHandler(newValue as FormProps);
  182. } else {
  183. looseHandler(newValue as FormProps);
  184. }
  185. };
  186. /// 严格模式
  187. const strictHandler = (values: FormProps) => {
  188. if (type !== "login") {
  189. if (!checkBoxValue) {
  190. Toast.show({
  191. content: t("form.readyAgreement"),
  192. });
  193. return;
  194. }
  195. values.birthday = dayjs(values.birthday).format("YYYY/MM/DD");
  196. }
  197. looseHandler(values);
  198. };
  199. const loginHandler = async (values: FormProps) => {
  200. return new Promise(async (resolve, reject) => {
  201. const loginResult = await loginApi(values).catch((error) => {
  202. let text = error ? t(`code.${error.data.code}`) : t(`code.${500}`);
  203. Toast.show({
  204. content: text,
  205. });
  206. });
  207. if (loginResult?.code === 200) {
  208. eventLogin();
  209. setCookies("Token", loginResult.data.token as string);
  210. setUserInfo(loginResult.data);
  211. resolve(loginResult);
  212. return loginResult;
  213. } else {
  214. reject();
  215. }
  216. });
  217. };
  218. /// 宽松模式
  219. const looseHandler = async (values: FormProps) => {
  220. // 请求
  221. Toast.show({
  222. icon: "loading",
  223. duration: 0,
  224. });
  225. // 注册
  226. if (type === "register") {
  227. values.user_name = `${values.first_name} ${values.last_name}`;
  228. delete values.first_name;
  229. delete values.last_name;
  230. const newValues = {
  231. ...values,
  232. referrer_code: sessionStorage.getItem("shareId") ?? undefined,
  233. channel_code: Local.getKey("channel_code") ?? undefined,
  234. // 轮盘邀请
  235. turntable_id: Number(sessionStorage.getItem("turntable_id")) ?? undefined,
  236. turntable_user_id: Number(sessionStorage.getItem("turntable_user_id")) ?? undefined,
  237. turntable_time: Number(sessionStorage.getItem("turntable_time")) ?? undefined,
  238. // 快玩id
  239. click_id: Local.getKey("ban_click_id") ?? undefined,
  240. };
  241. registerApi(newValues)
  242. .then(async (res) => {
  243. if (res.code === 200) {
  244. eventRegister();
  245. loginHandler({
  246. pwd: values.pwd,
  247. user_phone: values.user_phone,
  248. code_phone: values.code_phone,
  249. }).then(() => {
  250. router.replace("/recharge");
  251. Toast.clear();
  252. });
  253. }
  254. })
  255. .catch((error) => {
  256. console.log(`🚀🚀🚀🚀🚀-> in index.tsx on 257`, error);
  257. if (error.data?.code === 1017) {
  258. sessionStorage.removeItem("shareId");
  259. }
  260. Toast.show({
  261. content: t(`code.${error.data?.code ?? 500}`),
  262. });
  263. });
  264. } else {
  265. /// 登录
  266. loginHandler(values).then(() => {
  267. Toast.clear();
  268. const redirect = searchParams.get("redirect")
  269. ? `/${searchParams.get("redirect")}`
  270. : "/";
  271. router.replace(redirect);
  272. });
  273. }
  274. };
  275. const onConfirm = (value: PickerDate) => {
  276. const isChildren = dayjs().subtract(18, "year").isBefore(value);
  277. if (isChildren) {
  278. Toast.show({
  279. icon: "fail",
  280. content: t("form.NotSuitableForChildren"),
  281. });
  282. }
  283. };
  284. const ageValidator = (rule: any, value: PickerDate) => {
  285. const isChildren = dayjs().subtract(18, "year").isBefore(value);
  286. if (isChildren) {
  287. return Promise.reject(new Error(rule.message));
  288. } else {
  289. return Promise.resolve();
  290. }
  291. };
  292. // 手机号验证规则
  293. const checkMobile = (_: any, value: any) => {
  294. const reg = /^.{2}9/;
  295. if (!reg.test(value.realValue)) {
  296. return Promise.reject(new Error(t("form.phoneRules")));
  297. } else if (value.realValue.length < 10) {
  298. return Promise.reject(new Error(t("form.phoneMinReg")));
  299. } else {
  300. return Promise.resolve();
  301. }
  302. };
  303. return (
  304. <div className={clsx(styles.loginForm, "custom-form px-spacing-x py-spacing-y")}>
  305. <Form
  306. style={{
  307. "--border-bottom": "none",
  308. "--border-top": "none",
  309. "--border-inner": "none",
  310. }}
  311. ref={formRef}
  312. initialValues={params.current}
  313. onFinish={onFinish}
  314. footer={
  315. <ButtonOwn active className={clsx(styles.btn, "font-bold")}>
  316. {type === "login" ? t("form.loginText") : t("form.registerText")}
  317. </ButtonOwn>
  318. }
  319. >
  320. <Form.Item name="mobile" rules={[{ required: true, validator: checkMobile }]}>
  321. <MobileField />
  322. </Form.Item>
  323. <Form.Item
  324. name="pwd"
  325. label={
  326. <i className="iconfont icon-xiugaimima text-[.18rem] text-[#465461]"></i>
  327. }
  328. extra={
  329. <span className={spanClassName} onClick={() => setVisible(!visible)}></span>
  330. }
  331. layout="horizontal"
  332. rules={[
  333. { required: true, message: t("form.passwordReg") },
  334. { min: 6, max: 20, message: t("form.passwordMinReg") },
  335. ]}
  336. >
  337. <Input
  338. placeholder={t("form.password")}
  339. maxLength={20}
  340. type={visible ? "text" : "password"}
  341. />
  342. </Form.Item>
  343. {type !== "login" && isStrictMode ? (
  344. <>
  345. {/* <Form.Item
  346. name="user_name"
  347. label=""
  348. rules={[{ required: true, message: t("form.usernameReg") }]}
  349. >
  350. <Input placeholder={t("form.username")} />
  351. </Form.Item> */}
  352. <div className="flex items-center">
  353. <Form.Item
  354. name="first_name"
  355. className="mr-[.1rem] w-[1.4rem]"
  356. layout="horizontal"
  357. label={
  358. <i className="iconfont icon-yonghu1 text-[.18rem] leading-[1] text-[#465461]"></i>
  359. }
  360. rules={[{ required: true, message: t("form.usernameReg") }]}
  361. >
  362. <Input placeholder={"Insira seu nome"} />
  363. </Form.Item>
  364. <Form.Item
  365. className="flex-1"
  366. name="last_name"
  367. label=""
  368. rules={[{ required: true, message: t("form.usernameReg") }]}
  369. >
  370. <Input placeholder={"Insira seu sobrenome"} />
  371. </Form.Item>
  372. </div>
  373. <Form.Item
  374. name="birthday"
  375. clickable={false}
  376. trigger={"onConfirm"}
  377. arrowIcon={<i className={"iconfont icon-xiangyou1"}></i>}
  378. onClick={(e, datePickerRef: RefObject<DatePickerRef>) => {
  379. datePickerRef.current?.open();
  380. }}
  381. rules={[
  382. { required: true, message: t("form.birthdayReg") },
  383. {
  384. message: t("form.NotSuitableForChildren"),
  385. validator: ageValidator,
  386. },
  387. ]}
  388. >
  389. <DatePicker
  390. getContainer={null}
  391. min={dayjs().subtract(50, "year").toDate()}
  392. max={dayjs().toDate()}
  393. style={{ background: "#fff", color: "#000" }}
  394. >
  395. {(value) =>
  396. value ? (
  397. dayjs(value).format("YYYY/MM/DD")
  398. ) : (
  399. <span className={"text-[#ccc]"}>{t("form.birthday")}</span>
  400. )
  401. }
  402. </DatePicker>
  403. </Form.Item>
  404. <Form.Item
  405. name="email"
  406. layout="horizontal"
  407. label={
  408. <i className="iconfont icon-youjian text-[.13rem] leading-[1] text-[#465461]"></i>
  409. }
  410. rules={[
  411. { required: true, message: t("form.emailReg"), pattern: emailReg },
  412. ]}
  413. >
  414. <Input placeholder={t("form.email")} />
  415. </Form.Item>
  416. <Form.Item name="sex">
  417. <Radio.Group>
  418. <Radio
  419. value={0}
  420. className={"mr-[0.1rem]"}
  421. style={{ "--icon-size": "18px" }}
  422. >
  423. {t("form.sexMan")}
  424. </Radio>
  425. <Radio value={1} style={{ "--icon-size": "18px" }}>
  426. {t("form.sexWoman")}
  427. </Radio>
  428. </Radio.Group>
  429. </Form.Item>
  430. <Form.Item
  431. name="passport"
  432. layout="horizontal"
  433. label={
  434. <i className="iconfont icon-shenfengzheng text-[.18rem] leading-[1] text-[#465461]"></i>
  435. }
  436. rules={[{ required: true, message: t("form.cardReg"), pattern: neReg }]}
  437. >
  438. <Input placeholder={t("form.card")} maxLength={11} type={"text"} />
  439. </Form.Item>
  440. <Form.Item
  441. name="address"
  442. label=""
  443. rules={[{ required: true, message: t("form.addressReg") }]}
  444. >
  445. <TextArea placeholder={t("form.address")} maxLength={40} />
  446. </Form.Item>
  447. <div className={"flex px-[0.1rem]"}>
  448. <Checkbox
  449. block
  450. style={{ "--icon-size": "16px" }}
  451. checked={checkBoxValue}
  452. onChange={(value) => setCheckBoxValue(value)}
  453. ></Checkbox>
  454. <div className={"ml-[10px] select-none break-all text-[12px]"}>
  455. {t("form.agreement")}
  456. <Link href={"/preventLaunderMoney"} className={"text-[#11de68]"}>
  457. {t("form.moneyAgreement")}
  458. </Link>
  459. <Link href={"/terms"} className={"text-[#11de68]"}>
  460. {t("form.serverAgreement")}
  461. </Link>
  462. {t("form.agreementAnd")}
  463. <Link href={"/gamingPolicy"} className={"text-[#11de68]"}>
  464. {t("form.childrenAgreement")}
  465. </Link>
  466. </div>
  467. </div>
  468. </>
  469. ) : null}
  470. <div className="mb-[0.2rem] flex justify-end text-[0.12rem]">
  471. {type == "login" && (
  472. <>
  473. <Link className={"text-[#677787]"} href="/resetPhone">
  474. {t("LoginPage.forgetPwd")}
  475. </Link>
  476. {/* <Link className={"text-[#fff]"} href="/register">
  477. {t("LoginPage.registerGo")}
  478. </Link> */}
  479. </>
  480. )}
  481. </div>
  482. </Form>
  483. </div>
  484. );
  485. };
  486. export default FormComponent;